﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Tracy.CoreLib.Data
{
    public class RssFilter : ManagedXmlModel_CommonFunctions, IManagedXmlModelItem
    {
        #region 构造函数
        public RssFilter() : base() { }
        public RssFilter(string xml) : base(xml) { }
        public RssFilter(XmlNode node) : base(node) { }
        #endregion

        public string XmlNodeName
        {
            get { return Constants.CONST_TAG_ITEM; }
        }

        private int _priority = 0;
        public int Priority
        {
            get { return _priority; }
            set
            {
                _priority = value;
                Data[Constants.CONST_TAG_PRIORITY] = value.ToString();
            }
        }

        public string Title
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_TITLE)) return Data[Constants.CONST_TAG_TITLE];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_TITLE] = value;
            }
        }

        public string Description
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_DESCRIPTION)) return Data[Constants.CONST_TAG_DESCRIPTION];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_DESCRIPTION] = value;
            }
        }

        public string Folder
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_FOLDER)) return Data[Constants.CONST_TAG_FOLDER];
                else return "";
            }
            set { Data[Constants.CONST_TAG_FOLDER] = value; }
        }

        private Filter filter;

        public Filter Filter
        {
            get { if (filter == null)filter = new Filter(); return filter; }
            set { filter = value; }
        }

        public override string ToString()
        {
            return Title;
        }

        protected override void PreParseXml(XmlNode node)
        {
            Title = SelectNode(node, Constants.CONST_TAG_TITLE, true).InnerText;
            //description = SelectNode(node, Constants.CONST_TAG_DESCRIPTION, true).InnerText;
            Folder = SelectNode(node, Constants.CONST_TAG_FOLDER, true).InnerText;
            XmlNode priorityNode = SelectNode(node, Constants.CONST_TAG_PRIORITY, false);
            if (priorityNode != null)
            {
                Priority = Convert.ToInt32(priorityNode.InnerText);
            }
            else
            {
                Priority = 0;
            }
            XmlNode filterNode = SelectNode(node, Constants.CONST_TAG_FILTER, true);
            filter = new Filter(filterNode);
        }

        protected override void PostParseXml(XmlNode node)
        {
            //兼容旧版Filter
            if(Data.ContainsKey(Constants.CONST_TAG_FILTER))
            {
                Data.Remove(Constants.CONST_TAG_FILTER);
                string key = Constants.CONST_TAG_LEFTPOS + "@" + Constants.CONST_TAG_FILTER;
                if (Data.ContainsKey(key))
                {
                    Data.Remove(key);
                }
                key = Constants.CONST_TAG_RIGHTPOS + "@" + Constants.CONST_TAG_FILTER;
                if (Data.ContainsKey(key))
                {
                    Data.Remove(key);
                }
            }
            
        }

        protected override void PostGenerateXml(XmlDocument doc, XmlNode newNode)
        {
            //AppendTextChildNode(doc, newNode, Constants.CONST_TAG_TITLE, Title);
            //AppendTextChildNode(doc, newNode, Constants.CONST_TAG_DESCRIPTION, Description);
            //AppendTextChildNode(doc, newNode, Constants.CONST_TAG_FOLDER, Folder);
            newNode.AppendChild(filter.ToXmlNode(doc));
        }

        #region IKeyedItem Members

        public string Key
        {
            get { return Id.ToString(); }
        }

        #endregion
    }

    public class RssFilterProvider : XmlModelProvider_CommonFunctions, IXmlModelProvider
    {
        #region 构造函数
        public RssFilterProvider() : base() { }
        public RssFilterProvider(string xml) : base(xml) { }
        public RssFilterProvider(XmlNode node) : base(node) { }
        #endregion

        public IXmlModelItem GenerateChild(System.Xml.XmlNode node)
        {
            return new RssFilter(node);
        }

        public string XmlNodeName
        {
            get { return Constants.CONST_TAG_RSSFILTERLIST; }
        }

    }
}
